package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import mest.VO.cxj.PlanCostOrderRecordVO;
import mest.VO.cxj.PlanOtherIncomeRecordVO;
import mest.entity.PlanCostOrderRecord;
import mest.entity.PlanOtherIncomeRecord;
import mest.service.cxj.PlanOtherIncomeRecordService;
import mest.service.cxj.PlanOtherIncomeService;
import mest.service.system.DBService;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("planOtherIncomeRecord")
@CrossOrigin
public class PlanOtherIncomeRecordController {

    @Autowired
    private PlanOtherIncomeRecordService planOtherIncomeRecordService;

    @Autowired
    private DBService dbService;

    /**
     * 新增计划费用单记录
     *
     * @param poir
     * @return
     */
    @PostMapping("insert")
    public String insert(@RequestBody PlanOtherIncomeRecord poir) {
        String theirDb = dbService.getCompanyDB();
        Integer userId = dbService.getUserId(); //获取操作人ID
        ResultObj resultObj = null;
        try {
            poir.setEfId(userId);//设置当前操作人
            if (planOtherIncomeRecordService.insertPlanOtherIncomeRecord(poir, theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 查询全部计划费用单记录
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer poicId) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<PlanOtherIncomeRecordVO> planOtherIncomeRecordVOList = planOtherIncomeRecordService.selectAllPlanOtherIncomeRecordById(page, limit, poicId, theirDb);
            Integer count = planOtherIncomeRecordService.selectCountPlanOtherIncomeRecordById(poicId, theirDb);
            resultObj = new ResultObj(0, "success", count, planOtherIncomeRecordVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
